package com.gome.axwl.aoc.facade.impl;

import com.cloudcache.client.cluster.base.CloudCacheClient;
import com.github.pagehelper.PageInfo;
import com.gome.axwl.aoc.common.util.ErrorCode;
import com.gome.axwl.aoc.entity.dto.PageResultDto;
import com.gome.axwl.aoc.entity.exception.ZQException;
import com.gome.axwl.aoc.entity.po.ContractTemplatePo;
import com.gome.axwl.aoc.entity.zq.ZQBaseResult;
import com.gome.axwl.aoc.entity.zq.ZQContractImage;
import com.gome.axwl.aoc.entity.zq.ZQContractTemplate;
import com.gome.axwl.aoc.entity.zq.ZQTemplatePdf;
import com.gome.axwl.aoc.facade.ContractTemplateFacade;
import com.gome.axwl.aoc.facade.util.ZQCheckResponseUtil;
import com.gome.axwl.aoc.facade.util.ZQRequestUtil;
import com.gome.axwl.aoc.service.ContractTemplateService;
import com.gome.common.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by wanglongyu on 2018/7/16.
 */
@Service
public class ContractTemplateFacadeImpl implements ContractTemplateFacade{
    private final static String CONTRACT_REDIS_KEY = "CONTRACT_NO";
    @Autowired
    private ContractTemplateService contractTemplateService;

    @Autowired
    private ZQRequestUtil zqRequestUtil;

    @Override
    public PageResultDto<ContractTemplatePo> queryTemplate(String templateName, Integer pageNum, Integer pageSize) {
        PageInfo<ContractTemplatePo> pageInfo = contractTemplateService.queryTemplate(templateName, pageNum, pageSize);
        PageResultDto<ContractTemplatePo> pageResultDto=new PageResultDto<>();
        pageResultDto.setPageNum(pageInfo.getPageNum());
        pageResultDto.setPageSize(pageInfo.getPageSize());
        pageResultDto.setTotal(pageInfo.getTotal());
        pageResultDto.setPageCount(pageInfo.getPages());
        pageResultDto.setData(pageInfo.getList());
        return pageResultDto;
    }

    @Override
    public String download(String templateNo) throws ZQException {
        ZQTemplatePdf result = zqRequestUtil.downloadTemplate(templateNo);
        ZQCheckResponseUtil.validZQResponse(result);
        return result.getPdfUrl();
    }

    @Override
    public Integer update(String templateNo, String templateName, String property, Integer signWay) {
        return contractTemplateService.update(templateNo, templateName, property, signWay,new Date());
    }

    @Override
    public int delete(String templateNo) {
        return contractTemplateService.delete(templateNo,new Date());
    }

    @Override
    public List<ContractTemplatePo> queryAll() {
        return contractTemplateService.queryAll();
    }

    @Override
    public int addTemplate(String base64, String property, Integer signWay, String templateName) throws ZQException {
        //调用众签创建模板
        ZQContractTemplate zqct = zqRequestUtil.createTemplate(base64,templateName);
        ZQCheckResponseUtil.validZQResponse(zqct);
        ContractTemplatePo ctp = new ContractTemplatePo();
        ctp.setProperty(property);
        ctp.setSignWay(signWay);
        ctp.setTemplateName(templateName);
        ctp.setTemplateNo(zqct.getT_no());
        ctp.setTemplateJson(zqct.getContract_val());
        ctp.setCreateTime(new Date());
        return contractTemplateService.addTemplate(ctp);
    }

    @Override
    public ZQContractImage downloadEmptyContractImg(String t_no) throws ZQException {
        if(valid(t_no)){
            throw new ZQException(ErrorCode.PARAM_IS_NULL,ErrorCode.PARAM_IS_NULL_MSG);
        }
        ZQContractImage result = zqRequestUtil.downloadEmptyContractImg(t_no);
        ZQCheckResponseUtil.validZQResponse(result);
        return result;
    }
    /***
     * 验证所有参数不能为空
     * @param param 参数均为String
     * @return
     */
    private boolean valid(String... param) {
        for (int i = 0;i < param.length;i++) {
            if (StringUtil.isBlank(param[i])) {
                return true;
            }
        }
        return false;
    }
}
